package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.dto.ArticleDto;
import com.heima.article.dto.ArticleHomeDto;
import com.heima.article.dto.ArticleInfoDto;
import com.heima.article.entity.ApArticle;
import com.heima.article.entity.ApArticleContent;
import com.heima.article.entity.ApAuthor;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.IApArticleContentService;
import com.heima.article.service.IApArticleService;
import com.heima.article.service.IApAuthorService;
import com.heima.article.service.IStaticHtmlService;
import com.heima.common.dto.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author SpaceTraveller
 * @since 2022-11-07
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements IApArticleService {

    @Autowired
    private IApAuthorService authorService;

    @Autowired
    private IApArticleContentService contentService;

    @Autowired
    private IStaticHtmlService htmlService;
    @Override
    @Transactional
    public Long publishOrUpdate(ArticleDto articleDto) {
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(articleDto, apArticle);
        // 作者信息是没有,要从作者表里面提出来
        LambdaQueryWrapper<ApAuthor> query = new LambdaQueryWrapper<ApAuthor>().eq(ApAuthor::getId, articleDto.getAuthorId());
        ApAuthor author = authorService.getOne(query);
        apArticle.setAuthorId(author.getId());
        apArticle.setAuthorName(author.getName());
        //异步生成静态html,并更新url到数据库
        htmlService.createStaticHtml(apArticle,articleDto.getContent());
        // apArticle.setStaticUrl(); //我猜这个是通过接口传参数拿到的,
        this.saveOrUpdate(apArticle); // 不管原来有没有,现在是有了,id也肯定有,通过雪花算法自增
        LambdaQueryWrapper<ApArticleContent> queryContent = new LambdaQueryWrapper<>();
        queryContent.eq(ApArticleContent::getArticleId, articleDto.getId());
        ApArticleContent articleContent = contentService.getOne(queryContent);
        if (articleContent == null) {
            articleContent = new ApArticleContent();
            articleContent.setArticleId(apArticle.getId());
            articleContent.setContent(articleDto.getContent());
        }
        articleContent.setContent(articleDto.getContent());
        contentService.saveOrUpdate(articleContent);
        return apArticle.getId();
    }

    /**
     * @param dto
     * @param type 0 首次加载,1 加载最新,2 加载更多
     * @return
     */
    @Override
    public ResponseResult load(ArticleHomeDto dto, int type) {
        // 构造查询条件
        LambdaQueryWrapper<ApArticle> query = new LambdaQueryWrapper<>();
        // 通用条件size=10,dto.getIndex()只要第1页,直接传1
        Page<ApArticle> page = new Page<>(1, dto.getSize());
        if (type == 0 || type == 1) {
            // 前端maxTime传0,代表获取所有,
            // 这样取时间毫秒值倒序排列,取最新的前10条
            query.gt(ApArticle::getPublishTime, dto.getMaxTime());
        }
        if (type == 2) {
            // 向下滑动信息流加载更多,前端传了minTime
            query.lt(ApArticle::getPublishTime, dto.getMinTime());
        }
        // 频道判断,0代表所有频道,如果不为0,则需要添加频道条件
        if (dto.getChannelId() != 0) {
            query.eq(ApArticle::getChannelId, dto.getChannelId());
        }
        // 必须是未删除,未下架的文章
        query.ge(ApArticle::getIsDelete, false);
        query.ge(ApArticle::getIsDown, false);
        // 按发布时间倒序
        query.orderByDesc(ApArticle::getPublishTime);
        Page<ApArticle> list = this.page(page, query);
        return ResponseResult.okResult(list.getRecords());
    }


    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {
        //通过id拿到文章
        LambdaQueryWrapper<ApArticle> query = new LambdaQueryWrapper<>();
        query.eq(ApArticle::getId,dto.getArticleId());
        ApArticle article = this.getOne(query);
        //通过id拿到文章内容
        LambdaQueryWrapper<ApArticleContent> queryContent = new LambdaQueryWrapper<>();
        queryContent.eq(ApArticleContent::getArticleId,dto.getArticleId());
        ApArticleContent content = contentService.getOne(queryContent);
        Map<String, Object> infoMap = new HashMap<>();
        infoMap.put("config", article);
        infoMap.put("content", content);
        return ResponseResult.okResult(infoMap);
    }

    @Override
    public ResponseResult loadArticleBehavior(ArticleInfoDto dto) {
        return null;
    }
}
